<template>
	<view>
		<block v-for="(item, index) in list" :key="item.topicId">
			<view @click="jump(item)">
				<view class="post-item">
					<view class="post-list-item">
						<view @click.stop="toUser(item.sysUser.userId)">
							<u-avatar class="avatar" :src="item.sysUser.avatar" level-bg-color="#000000"></u-avatar>
						</view>
						<view class="center">
							<view style="display: flex;align-items: center;">
								<text class="username">{{ item.sysUser.nickName }}</text>
							</view>
							<view>
								<text class="time">{{ item.createTime}}</text>
							</view>
						</view>
					</view>
					<view class="post-content">
						<text class="post-text">{{item.topicContent}}</text>

						<!-- 帖子类型 -->
						<block>
							<block v-if="item.topicVideo!=null">
								<video :src="item.topicVideo" controls></video>
							</block>
							<!--一张图片-->
							<block v-if="imageLength(item.topicImgList)==1">
								<image :lazy-load="true" mode="aspectFill" class="img-style-1" :src="item.topicImgList"
									@tap.stop="previewImage(item.topicImgList, item.topicImgList)"></image>
							</block>
							<block v-if="imageLength(item.topicImgList)==2">
								<view class="img-style-2">
									<image :lazy-load="true" v-for="(mediaItem, index2) in imageList(item.topicImgList)"
										:key="index2" @tap.stop="previewImage(mediaItem, imageList(item.topicImgList))"
										mode="aspectFill" :src="mediaItem"></image>
								</view>
							</block>
							<block v-if="imageLength(item.topicImgList)==3">
								<view class="img-style-3">
									<image :lazy-load="true" v-for="(mediaItem, index2) in imageList(item.topicImgList)"
										:key="index2" @tap.stop="previewImage(mediaItem, imageList(item.topicImgList))"
										mode="aspectFill" :src="mediaItem"></image>
								</view>
							</block>
							<block v-if="imageLength(item.topicImgList)==4">
								<view class="img-style-4">

									<image :lazy-load="true" v-for="(mediaItem, index2) in imageList(item.topicImgList)"
										:key="index2" @tap.stop="previewImage(mediaItem, imageList(item.topicImgList))"
										mode="aspectFill" :src="mediaItem"></image>
								</view>
							</block>
						</block>
					</view>

					<!-- 底部 -->
					<view class="post-list-bottom">
						<view class="p-item margin50">
							<text class="iconfont" style="font-size: 30rpx;">&#xe605;</text>
							<text class="count">{{ item.readCount }}</text>
						</view>
						<view v-show="item.topicCollection==1" class="p-item"
							@click.stop="cancelCollection(item,index)">
							<u-icon name="heart-fill" color="#cc0000"></u-icon>
							<text class="count">{{ item.topicLikes }}</text>
						</view>
						<view v-show="item.topicCollection==0" class="p-item" @click.stop="addCollection(item,index)">
							<u-icon name="heart"></u-icon>
							<text class="count">{{ item.topicLikes }}</text>
						</view>
						<view class="p-item margin50">
							<text class="iconfont" style="font-size: 40rpx;">&#xe64b;</text>
							<text class="count">{{ item.topicCount }}</text>
						</view>

					</view>
				</view>
			</view>
		</block>

		<!-- 加载状态 -->
		<block v-if="list.length === 0 && loadStatus == 'nomore'">
			<u-empty margin-top="100" text="暂无内容" mode="favor"></u-empty>
		</block>
		<block v-else>
			<view style="margin: 30rpx 0;">
				<u-loadmore :status="loadStatus" />
			</view>
		</block>
	</view>
</template>

<script>
	import {
		likeEdit,
		readEdit
	}
	from "@/api/topic/topic.js"
	export default {
		name: 'post-list',
		props: {
			list: Array,
			loadStatus: String,
			uid: Number,
		},
		data() {
			return {

			};
		},

		computed: {
			imageLength() {
				return function(e) {
					if (e != null) {
						let imageList = e.split(",");
						let j = 0;
						for (let i = 0; i < imageList.length; i++) {
							j++;
						}
						return j
					} else {
						return 0;
					}

				}
			},
			imageList() {
				return function(e) {
					let imageList = e.split(',')
					return imageList
				}
			},
		},
		methods: {

			cancelCollection(item, index) {
				likeEdit(item.topicId, item.sysUser.userId, 0, 0).then(res => {
					if (res.code === 200) {
						this.list[index].topicCollection = 0;
						this.list[index].topicLikes--;
					}
				});
			},
			addCollection(item, index) {
				likeEdit(item.topicId, item.sysUser.userId, 1, 0).then(res => {
					if (res.code === 200) {
						this.list[index].topicCollection = 1;
						this.list[index].topicLikes++;
					}
				});
			},
			previewImage(url, urls) {
				uni.previewImage({
					current: url, // 当前显示图片的http链接
					urls: urls // 需要预览的图片http链接列表
				});
			},

			jump(e) {
				readEdit(e.topicId).then((res) => {
					this.$emit("reflash")
					uni.navigateTo({
						url: '/pages/home/homeSchoolCircle-details/index?item=' + encodeURIComponent(JSON
							.stringify(e))
					});
				})

			},

			toUser(uid) {
				uni.navigateTo({
					url: '/pages/user/home?uid=' + uid
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	video {
		width: 100%;
		height: 400rpx;
	}

	.post-item {
		background: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;

		.post-content {
			margin-top: 20rpx;

			.img-style-1 {
				display: block;
				width: 100%;
				height: 600rpx;
				border-radius: 5px;
				overflow: hidden;

			}

			.img-style-2 {
				display: flex;

				image {
					margin: 5rpx;

					width: 100%;
					height: 305rpx;
				}
			}

			.img-style-3 {
				display: flex;
				flex-wrap: wrap;

				image {
					width: 31.3%;
					height: 200rpx;
					margin: 0.6%;

				}
			}

			.img-style-4 {
				display: flex;
				flex-wrap: wrap;

				image {
					width: 48%;
					height: 320rpx;
					margin: 0.5%;

				}
			}
		}

		.address {
			display: flex;
			font-size: 20rpx;
			background-color: #F5F5F5;
			border-radius: 20rpx;
			display: inline-block;
			padding: 5rpx 20rpx;
			margin: 20rpx 0;

			.icon {
				margin-right: 5rpx;
			}
		}
	}

	.post-list-item {
		display: flex;
		align-items: center;

		.avatar {
			width: 85rpx;
			height: 85rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.center {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 24rpx;
			color: #999;

			.username {
				font-size: 32rpx;
				font-weight: 600;
				color: #616161;
			}
		}
	}

	.post-text {
		display: block;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 10;
		white-space: pre-wrap;
		overflow: hidden;
	}

	.post-list-bottom {
		display: flex;
		margin: 20rpx 0;

		.p-item {
			margin: 0 auto;
			color: #616161;
			display: flex;
			align-items: center;

			.count {
				margin-left: 10rpx;
				font-size: 28rpx;
			}
		}

		.p-item[hidden] {
			display: none !important;
		}
	}
</style>
